package com.jumi.microservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jumi.microservice.common.constant.DelFlag;
import com.jumi.microservice.dto.reponse.UserAuthExternalResponse;
import com.jumi.microservice.dto.reponse.UserCashExternalResponse;
import com.jumi.microservice.dto.reponse.UserExternalResponse;
import com.jumi.microservice.entity.JmRealnameAuthentication;
import com.jumi.microservice.entity.JmUser;
import com.jumi.microservice.mapper.JmRealnameAuthenticationMapper;
import com.jumi.microservice.mapper.JmUserMapper;
import com.jumi.microservice.service.IUserExternalService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

/**
 * @author scw
 * @date 2020/10/28
 */
@DubboService
public class UserExternalExternalController implements IUserExternalService {
    @Resource
    JmUserMapper jmUserMapper;
    @Resource
    JmRealnameAuthenticationMapper jmRealnameAuthenticationMapper;

    /**
     * 根据用户uid查询用户信息
     *
     * @param uid
     * @return
     */
    @Override
    public UserExternalResponse getUserByUid(Integer uid) {
        //查询信息
        QueryWrapper<JmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", uid);
        JmUser jmUser = jmUserMapper.selectOne(queryWrapper);
        if (jmUser == null) {
            return null;
        }
        UserExternalResponse userExternalResponse = new UserExternalResponse();
        userExternalResponse.setNickName(jmUser.getName());
        userExternalResponse.setTrueName(jmUser.getTruename());
        userExternalResponse.setHeadUrl(jmUser.getAvatar());
        userExternalResponse.setMobile(jmUser.getMobile());
        userExternalResponse.setLevel(jmUser.getLevel().getValue().byteValue());
        return userExternalResponse;
    }

    /**
     * 根据手机号查询用户uid
     *
     * @param mobile
     * @return
     */
    @Override
    public Integer getUidByMobile(String mobile) {
        //查询信息
        QueryWrapper<JmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        //queryWrapper.eq("status", DelFlag.DELETE);
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 1");
        JmUser jmUser = jmUserMapper.selectOne(queryWrapper);
        if (jmUser == null) {
            return null;
        }
        return jmUser.getId();
    }

    /**
     * 根据用户uid查询用户实名认证信息
     *
     * @param uid
     * @return
     */
    @Override
    public UserAuthExternalResponse getUserAuthByUid(Integer uid) {
        //查询信息
        QueryWrapper<JmRealnameAuthentication> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("status", DelFlag.DELETE);
        queryWrapper.orderByDesc("id");
        JmRealnameAuthentication jmRealnameAuthentication = jmRealnameAuthenticationMapper.selectOne(queryWrapper);
        if (jmRealnameAuthentication == null) {
            return null;
        }
        UserAuthExternalResponse userAuthExternalResponse = new UserAuthExternalResponse();
        userAuthExternalResponse.setAliCode(jmRealnameAuthentication.getAliCode());
        userAuthExternalResponse.setBankCard(jmRealnameAuthentication.getBankCard());
        userAuthExternalResponse.setIdCard(jmRealnameAuthentication.getIdcard());
        userAuthExternalResponse.setTruename(jmRealnameAuthentication.getTruename());
        return userAuthExternalResponse;
    }

    /**
     * 根据用户uid查询用户实名认证信息
     *
     * @param uid
     * @return
     */
    @Override
    public UserCashExternalResponse getUserCashByUid(Integer uid) {
        return jmUserMapper.getUserCashByUid(uid);
    }
}
